package com.cis467.mrball;

import com.cis467.mrball.backend.GameManagement;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class HighScoreView extends Activity {
	
	private HighScore my_HighScores[];
	TableLayout tl;
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        GameManagement.new_highScore(new HighScore("SecretLevel",GameManagement.get_playerID(), 99.99));
        
        // tell system to use the layout defined in our XML file
        setContentView(R.layout.highscorelayout);
        try{
        	my_HighScores = GameManagement.get_myHighScores();

        // Get the TableLayout
        tl = (TableLayout) findViewById(R.id.highScoreTable); 
        
        //create tableRow for headings
        TableRow tr2 = new TableRow(this);
        TextView lTV = new TextView(this); 
        TextView tTV = new TextView(this);
        createView(tr2, lTV, "Level");
        createView(tr2, tTV, "Time");
        
        tl.addView(tr2);
        
        // Go through each item in the array
        for(int current = 0; current < my_HighScores.length; current++)
        {
        	// Create a TableRow
            TableRow tr = new TableRow(this);          
            // Create a TextView to house the time
            TextView timeTV = new TextView(this);
            // Create a TextView to house level name
            TextView levelTV = new TextView(this);
            
            // Fill in
            createView(tr, levelTV, (my_HighScores[current].getLevelName() + ""));
            createView(tr, timeTV, ((int)(my_HighScores[current].getTime()*100)/100.0 + ""));
            
            // Add the TableRow to the TableLayout
            tl.addView(tr);          
        }	
        }catch(Exception e){
        	// no highscores
        	e.printStackTrace();
            // Get the TableLayout
            tl = (TableLayout) findViewById(R.id.highScoreTable); 
            
            //create tableRow for headings
            TableRow tr2 = new TableRow(this);
            TextView lTV = new TextView(this);  
            createView(tr2, lTV, "No High Scores Yet");

            tl.addView(tr2);
        }
	}
    
    public void createView(TableRow tr, TextView t, String viewdata){
    	t.setText(viewdata);
    	//adjust the properties of the textView
    	t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    	t.setTextColor(Color.WHITE);
    	t.setBackgroundColor(Color.BLACK);
    	t.setPadding(20, 0, 0, 0);
    	tr.setPadding(0, 1, 0, 1);
    	tr.setBackgroundColor(Color.BLACK);
    	tr.addView(t); // add TextView to row.
    }
}
